<?php
include('functions.inc.php');
$link = db_connect();
?>
<html>
<head>
	<title>GobbleRSS - Statistics</title>
	<link href="css/gob.css" type="text/css" rel="stylesheet">
	<script type="text/javascript" src="js/prototype.js"></script>
	<script type="text/javascript" src="js/gob.js"></script>
	<script type="text/javascript" src="js/plotr/excanvas.js"></script>
	<script type="text/javascript" src="js/plotr/Plotr.js"></script>

	<script type="text/javascript">
	<?php
	$stats = array();
	$starred = array();
	$opened = array();

	$stats_query = "SELECT subId, title, link, state FROM articles ORDER BY pubDate DESC";
	$result = mysql_query($stats_query, $link);
	while ($row = mysql_fetch_array($result)) {
		if (!isset($stats['all'][$row['subId']])) {
			$stats['all'][$row['subId']] = 0;
			$stats['new'][$row['subId']] = 0;
			$stats['unread'][$row['subId']] = 0;
			$stats['starred'][$row['subId']] = 0;
			$stats['open'][$row['subId']] = 0;
		}
		$stats['all'][$row['subId']]++;
		if (strstr($row['state'], 'new') !== FALSE) {
			$stats['new'][$row['subId']]++;
		}
		if (strstr($row['state'], 'unread') !== FALSE) {
			$stats['unread'][$row['subId']]++;
		}
		if (strstr($row['state'], 'starred') !== FALSE) {
			$stats['starred'][$row['subId']]++;
			if (sizeof($starred) < $config['num_articles_latest_stats']) {
				$starred[$row['title']] = $row['link'];
			}
		}
		if (strstr($row['state'], 'open') !== FALSE) {
			$stats['open'][$row['subId']]++;
			if (sizeof($opened) < $config['num_articles_latest_stats']) {
				$opened[$row['title']] = $row['link'];
			}
		}
	}

	$stats_query = "SELECT id, title FROM subscriptions";
	$result = mysql_query($stats_query, $link);
	while ($sub = mysql_fetch_array($result)) {
		$subsTitle[$sub['id']] = $sub['title'];
	}

	?>
	var options = {
		padding: {left: 0, right: 0, top: 0, bottom: 0},
		barOrientation: 'horizontal',
		colorScheme: 'blue',
		xTicks: [
	<?php
			$unknown = array();
			ksort($stats['all']);
			foreach ($stats['all'] as $subId => $total) {
				$max = $subId;
			}
			for ($i=1; $i <= $max; $i++) {
				if ($subsTitle[$i] == '') {
					$unknown[$i] = TRUE;
					$stats['all'][$i] = 0;
					$stats['new'][$i] = 0;
					$stats['unread'][$i] = 0;
					$stats['starred'][$i] = 0;
					$stats['open'][$i] = 0;
					echo "		{v:$i, label:'[Deleted]'}, \n";
				} else {
					echo "		{v:$i, label:'".str_replace("'","\\'",$subsTitle[$i])."'}, \n";
				}
			}
			?>
		]
	};

	function go() {
		//var bar = new Plotr.BarChart('plotr_all', options);
		var bar = new Plotr.PieChart('plotr_all', options);
		<?php
		foreach ($stats as $stat => $subsStats) {
			echo "var dataset = {\n";

			arsort($subsStats);
			$subTotal = 0;
			foreach ($subsStats as $subId => $total) {
				$subTotal += $total;
			}

			echo "	'$stat': [";
			foreach ($subsStats as $subId => $total) {
				if ($total > ($subTotal/100) && !isset($unknown[$subId])) {
					echo "[$subId, $total], ";
				}
			}
			echo "]\n";

			echo "};\n";

			echo "bar.reset();\n";
			echo "bar.addDataset(dataset);\n";
			echo "bar.render('plotr_".$stat."', options);\n\n";
		}
		?>
	}
	</script>
</head>

<body onload="go()">

<div style="padding-top: 10px; float: right">
	<?php include ('menu.inc.php') ?>
</div>

<h1>Articles & Feeds Statistics</h1>

<div><h2>All</h2><canvas id="plotr_all" height="800" width="800"></canvas></div>
<div><h2>New</h2><canvas id="plotr_new" height="800" width="800"></canvas></div>
<div><h2>Unread</h2><canvas id="plotr_unread" height="800" width="800"></canvas></div>
<div>
	<h2>Starred</h2>
	<canvas id="plotr_starred" height="800" width="800"></canvas>
	<h3>Last Starred</h3>
	<?php
		foreach ($starred as $title => $link) {
			echo "<a href=\"$link\">$title</a><br/>\n";
		}
	?>
</div>
<div>
	<h2>Opened</h2>
	<canvas id="plotr_open" height="800" width="800"></canvas>
	<h3>Last Opened</h3>
	<?php
		foreach ($opened as $title => $link) {
			echo "<a href=\"$link\">$title</a><br/>\n";
		}
	?>
</div>

</body>
</html>
